'''※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
File Name: system.py
Author: GID5564
Description: 系统信息
Version: 1.0
Created Time: 04/10/23-19:30:31
※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※'''
  
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import psutil
import datetime


class SystemInformation:
    def __init__(self):
        pass
    
    #开机时间
    def get_boot_time(self):
        return datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H: %M: %S")
    
    #cpu逻辑、物理个数
    def get_cpu_counts(self,logical=True):
        return psutil.cpu_count(logical)
    
    #cpu使用率
    def get_cpu_usage(self):
        cpu_usage = psutil.cpu_percent(interval=1)
        return f"{cpu_usage}%"
        
    #内存使用率
    def get_memory_usage(self):
        memory_usage = psutil.virtual_memory()
        return f"{memory_usage.percent}%"
        
    #磁盘使用率
    def get_disk_usage(self):
        disk_usage = psutil.disk_usage('/')        
        return f"{disk_usage.percent}%"
        
    #磁盘状态
    def get_disk_status(self):
        disk_usage = psutil.disk_usage('/')        
        total=f'{disk_usage.total / 1024 ** 3:.2f}GB'
        free=f'{disk_usage.free / 1024 ** 3:.2f}GB'
        used=f'{disk_usage.used / 1024 ** 3:.2f}GB'
        return f"F:{free}/U:{used}/T:{total}"

    ##获取网络上的信息须要有权限
    #接收的数据量
    def net_receive_counts(self):
        net_info = psutil.net_io_counters()
        return f"{net_info.bytes_recv / 1024 ** 2:.2f}MB"
    
    #发送的数据量
    def net_send_counts(self):
        net_info = psutil.net_io_counters()
        return f"{net_info.bytes_sent / 1024 ** 2:.2f}MB"
        
    #查看所有进程
    def view_proc(self):
        for pnum in psutil.pids():
            p = psutil.Process(pnum)
            pid=pnum
            proc_name=p.name()
            status=p.status()
            mem_percent=p.memory_percent()
            create_time=datetime.datetime.fromtimestamp(p.create_time()).strftime("%Y-%m-%d %H:%M:%S")
            #exe=p.exe()
            #cwd=p.cwd()
            thread_num=p.num_threads()
            ret=f"PID:{pnum} 进程名:{proc_name} 状态:{status} 内存使用率:{mem_percent:.2f}% 升启线程数:{thread_num} 创建时间:{create_time}\n"
            print(ret)
            
            



        
        
'''
if __name__ == "__main__":
    si=SystemInformation()
    print(si.view_proc())
    
'''
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        